//
//  MoreCollectionViewController.m
//  SYiDaiy
//
//  Created by dllo on 16/9/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MoreCollectionViewController.h"
#import "TAMacro.h"
#import "AFNetworking.h"
#import "BrandTopViewController.h"
#import "MoreESRootClass.h"
#import "MoreBanner_Brand.h"
#import "MoreGrids.h"
#import "MoreGoodies.h"
#import "MoreDetailViewController.h"
@interface MoreCollectionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,strong) UICollectionView *moreCollection;
@property (nonatomic,strong) MoreESRootClass *moreESRootClass;
@property (nonatomic,strong) UICollectionViewFlowLayout *flow;
@end

@implementation MoreCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH / 375 * 120);
    [self createmoreCollection];
}

- (void)createmoreCollection
{
    UICollectionViewFlowLayout *flow = [UICollectionViewFlowLayout new];
    CGFloat width = (SCREEN_WIDTH - 20) / 2;
    flow.itemSize = CGSizeMake(width, SCREEN_WIDTH / 375 * 120 - 10);
    flow.minimumInteritemSpacing = 10;
    flow.minimumLineSpacing = 10;
    flow.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    _moreCollection = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flow];
    [self.moreCollection reloadData];
    [self moreNetworkGet];
    _moreCollection.dataSource = self;
    _moreCollection.delegate = self;
    _moreCollection.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_moreCollection];
    [_moreCollection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"moreCollectionCell"];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"moreCollectionCell" forIndexPath:indexPath];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, (SCREEN_WIDTH - 20) / 2, SCREEN_WIDTH / 375 * 120 - 10)];
    [imageView sd_setImageWithURL:[NSURL URLWithString:_moreESRootClass.grids[1].goodies[indexPath.row + 2].pic]];
    [cell addSubview:imageView];
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 2;
}


- (void)moreNetworkGet
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *requestURLString = @"http://watch-cdn.idailywatch.com/app/watch/honored?ver=iphone&app_ver=15&lang=zh-Hans-CN";
    
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.requestSerializer.timeoutInterval = 0;
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager GET:requestURLString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSDictionary dictionary];
        dic = responseObject;
        _moreESRootClass = [MoreESRootClass modelWithDic:dic];
        
        [self.moreCollection reloadData];
        
        


        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",task.currentRequest);
        NSLog(@"请求失败%@",error);
    }];
    
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MoreDetailViewController *moreDetailVC = [MoreDetailViewController new];
    [self.navigationController pushViewController:moreDetailVC animated:YES];
    moreDetailVC.url = _moreESRootClass.grids[1].goodies[indexPath.row + 2].url;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
